<?php
/**
*   author:yuchong
 *  date:20150502
 *  function:font
 */

class Member_model extends MY_Model {

    public $m;

    public function __construct() {
        parent::__construct();
        $this->_table = "db_member";
    }


    /**
    *   注册的时候用户名检查
     * 1:不能为空
     * 2:不能包含禁止的用户名
     * 3:检查是否存在
     */
    public function check_username($username) {
        $this->username = $username;
        if(empty( $username )) {
            $this->error = "用户名不能为空1";
            return FALSE;
        }

        //TODO  查询setting表


        if($this->exists( "username", $username )) {
            $this->error = "用户名已经存在";
            return FALSE;
        }
        return TRUE;
    }

    /**
    *   邮箱检查
     * 1:检查邮箱格式
     * 2:检查邮箱是否存在
     */
    public function check_email($email) {
        $this->email = $email;
        if(!preg_match( "/^[\\w\\-\\.]+@[\\w\\-\\.]+(\\.\\w+)+\$/", $email )) {
            $this->error = "E-mail格式不正确";
            return FALSE;
        }
        if($this->exists( "email", $email )) {
            $this->error = "E-mail已经存在";
            return FALSE;
        }
        return TRUE;
    }

    /**
    *   用户编辑
     */
    public function edit($userid, $data) {
        return $this->update( $data, array("userid" => $userid ) );
    }

    /**
    *   用户更改密码
     *  1:获取用户信息，判断用户是否存在
     * 2:判断原密码是否正确
     * 3:设置新密码
     */
    public function password($userid, $password, $last_password, $isMd5 = FALSE) {
        $r = $this->get_by_field('userid',$userid, "username, password, email, salt");
        if(!$r) {
            $this->error = "用户不存在";
            return FALSE;
        }
        if(!$this->check_password( $r['password'], $last_password, $r['salt'], $isMd5 )) {
            $this->error = "原密码不正确";
            return FALSE;
        }
        $this->userid = $r['userid'];
        $this->username = $r['username'];

        return $this->set_field( "password", self::make_password( $password, $r['salt']), array("userid"=>$userid) );
    }

    /**
    *   更换邮箱
     * 1:获取用户信息，判断用户是否存在
     * 2:判断密码是否正确
     * 3:判断邮箱是否存在
     * 4:更新邮箱
     */
    public function email($userid, $password, $email) {
        $r = $this->get_by_field('userid', $userid, "username,password,email,salt");
        if(!$r) {
            $this->error = "用户不存在";
            return FALSE;
        }
        if(!$this->check_password( $r['password'], $password, $r['salt'] )) {
            $this->error = "密码不正确";
            return FALSE;
        }
        if(($exists = $this->get_by_field( "email", $email )) && $exists['userid'] != $this->_userid) {
            $this->error = "E-mail已经存在";
            return FALSE;
        }
        $this->userid = $r['userid'];
        $this->username = $r['username'];
        $this->email = $email;
        return $this->set_field( "email",  $email, array('userid',$userid) );
    }


    /**
    *   用户注册
     */
    public function register($data) {
        if($this->userid) {
            $userid = $this->userid;
        } else {
            if(!$this->check_username( $data['username'] )) {
                return FALSE;
            }
            if(!$this->check_email( $data['email'] )) {
                return FALSE;
            }
            $data['salt']     = mt_rand( 100000, 999999 );
            $data['regip']    = IP;
            $data['regtime']  = TIME;
            $data['password'] = self::make_password( $data['password'], $data['salt'] );
            $userid = $this->insert($data);
            if($userid) {
                $detail_data['userid'] = $userid;
                $this->load->model("member_detail_model");
                $this->member_detail_model->insert( $detail_data );
            }
        }
        if($userid) {
            return $userid;
        }
        return FALSE;
    }



    /**
    *   用户登录
     * 1:获取用户信息，检查用户是否存在
     * 2:检查登录记录，判断是否撞库
     * 3:密码检查
     * 4:更新登录信息
     * 5:更新登录次数
     * 6:设置cookie
     * 7:添加登录记录
     */
    public function login($username, $password, $cookietime = 3600) {
        $this->m = $this->db->from($this->_table)->where('username', $username)->get()->row_array();;

        //用户检查
        if(!$this->m) {
            $this->error = "{$username} 不存在";
            return FALSE;
        }

        //记录检查
        $this->load->model("member_login_log_model");
        if(!$this->member_login_log_model->valid($username)){
            $this->error = "登录失败次数过多，已被系统锁定";
            return FALSE;
        }

        //密码检查
        if(!$this->check_password( $this->m['password'], $password, $this->m['salt'] )) {
            $this->member_login_log_model->insert(array("username" => $username,"ip"=>IP, 'time'=>TIME, "succeed" => 0 ));
            $this->error = "密码错误";
            return FALSE;
        }

        //更新最新登陆信息
        $this->update( array("lastloginip" => IP, "lastlogintime" => TIME ), array("userid"=>$this->m['userid']) );
        //更新登录次数
        $this->set_inc( "logintimes", array('userid', $this->m['userid']) );

        $cookietime += time();
        setcookie( "auth", str_encode( $this->m['userid'] . "\t" . $this->m['username'], get_config_item('authkey') ), $cookietime, '/' );
        setcookie( "userid", $this->m['userid'], $cookietime, '/' );
        setcookie( "username", $this->m['username'], $cookietime, '/' );
        setcookie( "rememberusername", $this->m['username'], time()+2592000, '/' );


        $this->username = $username;
        $this->userid   = $this->m['userid'];

        $this->member_login_log_model->insert(array("username" => $username,"ip"=>IP, 'time'=>TIME, "succeed" => 1 ));

        return true;
    }

    /**
    *   用户退出，消除cookie
     */
    public function logout() {
        $cookietime = time() - 3600;
        setcookie( "auth", '', $cookietime, '/' );
        setcookie( "userid", '', $cookietime, '/' );
        setcookie( "username", '', $cookietime, '/' );
        setcookie( "rememberusername", '', $cookietime, '/');
    }


    /**
    *   用户密码加密,采用随机字符串加密
     */
    public static function make_password($password, $salt) {
        return md5( $password . $salt);
    }

    /**
    *   验证码检测
     * 1:普通验证
     * 2:加随机码验证
     */
    private function check_password($password, $inputpwd, $salt = "", $isMd5 = FALSE) {
        //$md5pass = $isMd5 ? $inputpwd : md5( $inputpwd );
        $md5pass = $isMd5 ?  md5( $inputpwd ) :$inputpwd;
        if($password == $md5pass) {
            return TRUE;
        }
        if($password == md5( $md5pass . $salt )) {
            return TRUE;
        }
        return FALSE;
    }

    /**
     *   字段验证
     */
    public function validate($data) {
        if($this->exists( "username", $data['username'] )) {
            return FALSE;
        }
        if($this->exists( "email", $data['email'] )) {
            return FALSE;
        }
        return TRUE;
    }

}

?>
